﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Water.Core;
using Water.Data.Infrastructure;
using Water.Model;
using NHibernate.Linq;


namespace Water.Data.Repositories
{
    public class MenuRepository : NhRepositoryBase<Menu>
    {
         private readonly IUnitOfWork uow;

         public MenuRepository(ISession session, IUnitOfWork uow)
            : base(session)
        {
            this.uow = uow;
        }

        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
         public IList<Menu> LoadAll(Expression<Func<Menu, bool>> where, int pageIndex, int pageSize, 
             params IOrderByExpression<Menu>[] orderByExpressions)
        {
            var query = GetAll().Where(where);
            if (orderByExpressions != null && orderByExpressions.Any())
            {
                query = query.OrderBy(orderByExpressions);
            }
            query = query.Fetch(e => e.NodeID);
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return query.ToList();
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
         public int LoadCount(Expression<Func<Menu, bool>> where)
        {

            var query = GetMany(where);
            return query.Count();
        }
        
    }
}
